package com.opages.mvc.adapter.consul.model;

import com.opages.mvc.adapter.common.properties.ConfigurationProperties;
import com.opages.mvc.adapter.common.utils.ServerUtils;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.PostConstruct;
import javax.management.MalformedObjectNameException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * consul客户端配置信息
 * @author daizhong.liu
 */
@ConfigurationProperties("spring.mvc.consul.discovery")
public class ConsulDiscoveryProperties {
	/**
	 * 访问服务器时要使用的主机名。
	 * Hostname to use when accessing server.
	 */
	private String hostname;

	/**
	 * 端口
	 * Port to register the service under (defaults to listening port).
	 */
	private Integer port;
	/**
	 * 是注册http服务还是https服务
	 * Whether to register an http or https service.
	 */
	private String scheme = "http";
	/**
	 * 是否使用acl
	 */
	@Value("${consul.token:${CONSUL_TOKEN:${spring.mvc.consul.token:${SPRING_CLOUD_CONSUL_TOKEN:}}}}")
    private String aclToken;
	/**
	 * 使用标签作为元数据，默认为true。
	 * Use tags as metadata, defaults to true.
	 */
    private boolean tagsAsMetadata = true;
    /**
     * 注册服务时要使用的标签。
     * Tags to use when registering service.
     */
    private List<String> tags = new ArrayList<>();

    /**
     * 注册服务时要使用的元数据。
     * Metadata to use when registering service.
     */
    private Map<String, String> metadata = new LinkedHashMap<>();
    /**
     * 是否启用服务发现
     * Is service discovery enabled?
     */
    private boolean enabled = true;
    /**
     * 服务名称
     * Service name.
     */
	private String serviceName;

	/**
	 * 唯一实例ID
	 * Unique service instance id.
	 */
	private String instanceId;

	/**
	 * 服务实例区域
	 * Service instance zone.
	 */
	private String instanceZone;

	/**
	 * 服务实例组。
	 * Service instance group.
	 */
	private String instanceGroup;
	/**
	 * 注册服务时，主机名是否包含在默认实例id中。
	 * Whether hostname is included into the default instance id when registering service.
	 */
	private boolean includeHostnameInInstanceId = false;
	/**
	 * 使用IP地址而不是域名
	 * Use ip address rather than hostname during registration.
	 */
	private boolean preferIpAddress = false;

	/** Source of how we will determine the address to use. */
	private boolean preferAgentAddress = false;

	/**
	 * Enable tag override for the registered service.
	 */
	private Boolean enableTagOverride;
	/**
	 * 服务实例区域来自metadata元数据。允许更改元数据标签名称。
	 * Service instance zone comes from metadata. This allows changing the metadata tag name.
	 */
	private String defaultZoneMetadataName = "zone";
	/**
	 * 注销多少时间后清除
	 **/
	private String deregisterCriticalServiceAfter="30m";
	/**
	 * 心跳时间,s/秒 m/分钟 h/小时
	 **/
	private String ttl="10s";
	@PostConstruct
	public void init() {
		//未设置客户端IP地址则获取本地IP地址
		if (hostname == null || "".equals(hostname)) {
			String ipv4EndPoints = ServerUtils.getIPV4EndPoints();
			if(!"".equals(ipv4EndPoints)) {
				hostname = ipv4EndPoints;
			}
		}
		//未设置客户端端口,则获取本地容器端口
		if(this.port == null || this.port.intValue() <= 0) {
			try {
				String port = ServerUtils.getPort();
				if (!"".equals(port)) {
					this.port = Integer.parseInt(port);
				}
			} catch (MalformedObjectNameException e) {
				e.printStackTrace();
			}
		}
	}

    @Deprecated
    public boolean isTagsAsMetadata() {
        return this.tagsAsMetadata;
    }

    public String getAclToken() {
        return aclToken;
    }

	public List<String> getTags() {
		return tags;
	}

	public void setTags(List<String> tags) {
		this.tags = tags;
	}

	public Map<String, String> getMetadata() {
		return metadata;
	}

	public void setMetadata(Map<String, String> metadata) {
		this.metadata = metadata;
	}

	public boolean isEnabled() {
		return enabled;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public String getServiceName() {
		return serviceName;
	}

	public void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}

	public String getInstanceId() {
		return instanceId;
	}

	public void setInstanceId(String instanceId) {
		this.instanceId = instanceId;
	}

	public String getInstanceZone() {
		return instanceZone;
	}

	public void setInstanceZone(String instanceZone) {
		this.instanceZone = instanceZone;
	}

	public String getInstanceGroup() {
		return instanceGroup;
	}

	public void setInstanceGroup(String instanceGroup) {
		this.instanceGroup = instanceGroup;
	}

	public void setTagsAsMetadata(boolean tagsAsMetadata) {
		this.tagsAsMetadata = tagsAsMetadata;
	}

	public void setAclToken(String aclToken) {
		this.aclToken = aclToken;
	}

	public boolean isIncludeHostnameInInstanceId() {
		return includeHostnameInInstanceId;
	}

	public void setIncludeHostnameInInstanceId(boolean includeHostnameInInstanceId) {
		this.includeHostnameInInstanceId = includeHostnameInInstanceId;
	}

//	public String getIpAddress() {
//		return ipAddress;
//	}
//
//	public void setIpAddress(String ipAddress) {
//		this.ipAddress = ipAddress;
//	}

	public String getHostname() {
		return hostname;
	}

	public void setHostname(String hostname) {
		this.hostname = hostname;
	}

	public Integer getPort() {
		return port;
	}

	public void setPort(Integer port) {
		this.port = port;
	}

	public boolean isPreferIpAddress() {
		return preferIpAddress;
	}

	public void setPreferIpAddress(boolean preferIpAddress) {
		this.preferIpAddress = preferIpAddress;
	}

	public boolean isPreferAgentAddress() {
		return preferAgentAddress;
	}

	public void setPreferAgentAddress(boolean preferAgentAddress) {
		this.preferAgentAddress = preferAgentAddress;
	}

	public Boolean getEnableTagOverride() {
		return enableTagOverride;
	}

	public void setEnableTagOverride(Boolean enableTagOverride) {
		this.enableTagOverride = enableTagOverride;
	}

	public String getScheme() {
		return scheme;
	}

	public void setScheme(String scheme) {
		this.scheme = scheme;
	}

	public String getDefaultZoneMetadataName() {
		return defaultZoneMetadataName;
	}

	public void setDefaultZoneMetadataName(String defaultZoneMetadataName) {
		this.defaultZoneMetadataName = defaultZoneMetadataName;
	}

	public String getDeregisterCriticalServiceAfter() {
		return deregisterCriticalServiceAfter;
	}

	public void setDeregisterCriticalServiceAfter(String deregisterCriticalServiceAfter) {
		this.deregisterCriticalServiceAfter = deregisterCriticalServiceAfter;
	}

	public String getTtl() {
		return ttl;
	}

	public void setTtl(String ttl) {
		this.ttl = ttl;
	}
}
